package com.cbd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cbd.dao.CbdpactDao;
import com.cbd.pojo.Cbdpact;
@Repository
public class CbdpactDaoImpl implements CbdpactDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public List<Cbdpact> findCompanypactByNO(String NO) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from Cbdpact where pactNO like ? and type = false");
		query.setParameter(0, "%"+NO+"%");
		return query.list();
	}

	@Override
	public int goOnCompanypact(Cbdpact companypact) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateCompanypact(int companyId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addCbdpact(Cbdpact cbd) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		session.save(cbd);
		return getIDByPactNo(cbd.getPactNO());
	}

	@Override
	public int getIDByPactNo(String cbdPactNo) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query query =session.createQuery("from Cbdpact where pactNO = ?");
		query.setParameter(0,cbdPactNo );
		query.uniqueResult();
		Cbdpact cbdPact =(Cbdpact)query.uniqueResult();
		return cbdPact.getPactID();
	}

	@Override
	public void canclePactByID(int parseInt) {
		Session session =sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Cbdpact set type = ? where pactID = ? ");
		query.setParameter(0, true);
		query.setParameter(1, parseInt);
		query.executeUpdate();
	}

	@Override
	public Cbdpact getCbdPactByID(int id) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Cbdpact where pactID = ?");
		query .setParameter(0, id);
		return (Cbdpact) query.uniqueResult();
	}

	

}
